//
//  MusicDataBase.m
//  老司机发车了标准版
//
//  Created by SuperNing on 16/8/11.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "MusicDataBase.h"


static MusicDataBase *_manager = nil;

@implementation MusicDataBase

+ (instancetype)shareMusicDataBase{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[MusicDataBase alloc] init];
        
    });
    return _manager;
}

- (void)openDB{
    
    NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSString *dataBasePath = [docuPath stringByAppendingString:@"/music.db"];
    NSLog(@"数据库路径---%@", dataBasePath);
    _dataBase = [FMDatabase databaseWithPath:dataBasePath];

}

- (void)createTableWithTableName:(NSString *)tableName{
    
    if (_dataBase == nil) {
        [self openDB];
    }
    
    _tableName = tableName;
    
    if (_dataBase.open == YES) {
        
        NSString *creatTableSqlite =  [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, SongName text, SingerName text, SongImage text, Lrcy text, SongUrl text)", tableName];
        BOOL result = [_dataBase executeStatements:creatTableSqlite];
        if (result == YES) {
            NSLog(@"建表成功");
        } else {
            NSLog(@"创建table出错或table已经存在");
        }
        [_dataBase close];
    }
    
}

// 添加数据
- (void)insertDataWithModel:(PlayerModel *)playmodel{
    
    if ([_dataBase open] == YES) {
        
        NSString *insertsqlite = [NSString stringWithFormat:@"insert into %@ (ID, SongName, SingerName, SongImage, Lrcy, SongUrl) values (?, ?, ?, ?, ?, ?)", _tableName];
        BOOL success = [_dataBase executeUpdate:insertsqlite,playmodel.song_id,playmodel.title,playmodel.author,playmodel.pic_radio,playmodel.lrclink,playmodel.file_link];
        if (success == YES) {
            
            NSLog(@"插入成功");
        }
    }
    
}

// 删除数据
- (void)deleteDataWithTableName:(NSString *)tableName songid:(NSString *)songid{
    
    if ([_dataBase open] == YES) {
        NSString *deletesqlite = [NSString stringWithFormat:@"delete from %@ where ID = ?", tableName];
        
        BOOL result = [_dataBase executeUpdate:deletesqlite,songid];
        if (result == YES) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
    }
}

// 查询数据
- (NSMutableArray *)selectOneDataWithTableName:(NSString *)tableName{
   
    NSMutableArray *allDataModels = [NSMutableArray array];
    if ([_dataBase open] == YES) {
        
        NSString *selectAll = [NSString stringWithFormat:@"select * from %@", tableName];
        
        FMResultSet *resultSet = [_dataBase executeQuery:selectAll];
        
        while ([resultSet next] == YES) {
            PlayerModel *playmodel = [[PlayerModel alloc] init];

            playmodel.song_id = [resultSet stringForColumn:@"ID"];
            playmodel.title = [resultSet stringForColumn:@"SongName"];
            playmodel.author = [resultSet stringForColumn:@"SingerName"];
            playmodel.pic_radio = [resultSet stringForColumn:@"SongImage"];
            playmodel.file_link = [resultSet stringForColumn:@"SongUrl"];
            playmodel.lrclink = [resultSet stringForColumn:@"Lrcy"];
            [allDataModels addObject:playmodel];
        }
        
    }
    return allDataModels;
}











@end
